package JMM;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * 演示重拍序的现象
 */
@SpringBootTest(classes = OutOfOrderExecutor.class)
public class OutOfOrderExecutor {
    private static int x = 0,y= 0;
    private static int a = 0,b = 0;

    @Test
    public void test() throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                a = 1;
                x = b;
            }
        });

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                b = 1;
                y = a;
            }
        });

        thread.start();
        thread1.start();
        thread.join();
        thread1.join();

        System.out.println("x = " +x+",y = "+y);
    }

}
